package com.question.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.tomcat.util.digester.SetPropertiesRule;
import org.apache.xmlbeans.XmlException;

import com.question.dao.TopicDao;
import com.question.dao.daoFactory.DaoFactory;
import com.question.domain.Topic;
import com.question.utils.MyProperties;

public class test1 {
	
	private static MyProperties properties = new MyProperties();
	private static TopicDao td = DaoFactory.getInstance().geTopicDao();
	
	//用于纪录最小的单选题
	 int minSingle= 0;
	//用于纪录最大的单选题
	 int maxSingle= 0;
	//用于纪录最小的多选题
	 int minMultiple= 0;
	//用于记录最大的多选题
	 int maxMultiple= 0;
	//用于记录最小的判断题
	 int minTF= 0;
	//用于记录最大的判断题
	 int maxTF= 0;
	
	public test1(){
		
		try {
			//用于纪录最小的单选题
			  minSingle = properties.getIntProperties("minSingle");
			//用于纪录最大的单选题
			  maxSingle = properties.getIntProperties("maxSingle");
			//用于纪录最小的多选题
			  minMultiple = properties.getIntProperties("minMultiple");
			  if(minMultiple==0){
				  minMultiple=maxSingle+1;
			  }
			//用于记录最大的多选题
			  maxMultiple = properties.getIntProperties("maxMultiple");
			  if(maxMultiple==0){
				  maxMultiple=maxSingle;
			  }
			//用于记录最小的判断题
			  minTF = properties.getIntProperties("minTF");
			  if(minTF==0){
				  minTF=maxMultiple+1;
			  }
			//用于记录最大的判断题
			  maxTF = properties.getIntProperties("maxTF");
			  if(maxTF==0){
				  maxTF=maxMultiple;
			  }
			 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//================================================================================
	//关于将获取整个文件数据的方法
	/**
	 * 说明：将docx文件中的所有内容转化为一个字符串输出
	 * 该文件有配置文件中获取
	 * 如果想获取文件请先在配置文件中配置该文件的路径
	 * 配置文件名：src目录下的topic.properties
	 * 
	 * @param file：要获取数据的文件名
	 * @return 返回docx文件中的数据
	 * @throws IOException
	 * @throws XmlException
	 * @throws OpenXML4JException
	 */
	public static String getTopicFromWord_docx(String file) throws IOException, XmlException, OpenXML4JException{
		String str = properties.getProperties(file);
		OPCPackage opcPackage = POIXMLDocument.openPackage(str);
		POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
		String docx = ((XWPFWordExtractor) extractor).getText();
		return docx;
	}
	

	/**
	 * 说明：将doc文件中的所有内容转化为一个字符串输出
	 * 该方法同上方法的不同点为该方法获取doc文件的数据，而上一个方法获取docx文件的数据
	 * 该文件有配置文件中获取
	 * 如果想获取文件请先在配置文件中配置该文件的路径
	 * 配置文件名：src目录下的topic.properties
	 * 
	 * @param file 要获取数据的文件名
	 * @return 返回docx文件中的数据
	 * @throws IOException
	 */
	public static String getTopicFromWord_doc(String file) throws IOException{
		String str = properties.getProperties(file);
		InputStream is = new FileInputStream(new File(str));
		WordExtractor ex = new WordExtractor(is);
		String doc = ex.getText();
		return doc;
	}
	//关于获取整个文件的方法
	//============================================================================
	
	//=============================================================================
	//有关单个题目的操作方法
	/*
	 * 
	 * 
	 * */
	
	/**
	 * 将一道题目的内容和选项分割开来
	 * 
	 * @return
	 */
	public String[] splitTopic(String topic){
		//用于存储最终的题目和选项
		String[] arr = new String[6];
		int i = 0;
		//用于暂时存储切割的元素
		String[] temp= new String[2];
		//按照该字符切割
		char ch = 'A';
		temp = topic.split(((char)(ch++))+"",2);
		arr[i++]=temp[0].trim();
		while(true) {
			try {
				temp = temp[1].split(ch+++"",2);
				arr[i++]=temp[0].trim();
			} catch (Exception e) {
				break;
			}
		}
		return arr;
	}
	/**
	 * 将字符串数组输入到题目中，字符串数组为专门匹配的数组
	 * 
	 * @param arr
	 * @param topic
	 */
	public void setTopic(String[] arr,Topic topic){
		topic.setQuestion(arr[0].split("\\.")[1].trim());
		topic.setA(arr[1]);
		topic.setB(arr[2]);
		topic.setC(arr[3]);
		topic.setD(arr[4]);
		topic.setE(arr[5]);
	}
	/**
	 * 判断由arr提供的数组表示的是数目类型的题目
	 * 
	 * @param object
	 * @return 返回0表示单选，返回1表示多选，返回2表示判断
	 */
	private int isWhatType(Topic topic) {
		if(topic.getC()==null)
			return 2;
		if(topic.getAnswer().length()>1)
			return 1;
		return 0;
	}
	
	 /**
	  * 添加一道单选题的properties的数据
	 */
	private void addSingle(){
		maxSingle++;
		minMultiple++;
		maxMultiple++;
		minTF++;
		maxTF++;
	}
	
	/**
	 * 添加一道多选题的properties的数据
	 */
	private void addMultplse(){
		maxMultiple++;
		minTF++;
		maxTF++;
	}
	
	/**
	 * 添加一道判断题的properties的数据
	 */
	private void addTF(){
		maxTF++;
	}
	
	//有关单个题目的分解方法
	//=================================================================
	/**
	 * 将一个含有多个单选答案的答案字符串转化为一个答案数组
	 * 
	 * @param answer：含有多个答案的答案字符串
	 * @return
	 */
	private char[] splitSingleAnswers(String answer){ 
		String all = answer.replaceAll("[^A-Z]","");
		char[] answers = all.toCharArray();
		return answers;
	}
	/**
	 * 将一个含有多个单选答案的答案字符串转化为一个答案数组
	 * 
	 * @param answer：含有多个答案的答案字符串
	 * @return
	 */
	private String[] splitMultplsAnswer(String answer){
		String all = answer.replaceAll("、", " ").replaceAll("[^A-Z]", " ");
		String[] answers = all.split("\\s+");
		return answers;
	}
	
	/**
	 * 将一个含有多个单选答案的答案字符串转化为一个答案数组
	 * 
	 * @param answer：含有多个答案的答案字符串
	 * @return
	 */
	private char[] splitTFAnswers(String answer){ 
		String all = answer.replaceAll("[^\\√\\×]","");
		char[] answers = all.toCharArray();
		System.out.println(answers.length);
		return answers;
	}

	private void setSingleTopic(String topic,String answer){
		String[] topics = splitTopics(topic);
		char[] answers = splitSingleAnswers(answer);
		if(topics.length-1!=answers.length){
			System.err.println("题目切割错误，请重新切割");
			return;
		}
		Topic t = new Topic();
		for(int i=0;i<topics.length-1;i++){
			setTopic(splitTopic(topics[i+1]), t );
			t.setAnswer(""+answers[i]);
			t.setType(isWhatType(t));
			addSingle();
			td.setTopic(t, maxSingle);
		}
		//System.out.println(topics.length);
	}
	
	private void setMultpleTopic(String topic,String answer){
		String[] topics = splitTopics(topic);
		String[] answers = splitMultplsAnswer(answer);
		Topic t = new Topic();
		for(int i=0;i<topics.length-1;i++){
			setTopic(splitTopic(topics[i+1]), t );
			t.setAnswer(answers[i+1]);
			t.setType(isWhatType(t));
			addMultplse();
			td.setTopic(t, maxTF);
		}
	}
				 
	private void setTFTopic(String topic,String answer){
		String[] topics = splitTopics(topic);
		char[] answers = splitTFAnswers(answer);
		System.out.println(topics.length+""+answers.length);
		Topic t = new Topic();
		for(int i=0;i<topics.length-1;i++){
			setTopic(splitTopic(topics[i+1]), t );
			t.setAnswer(""+answers[i]);
			t.setType(isWhatType(t));
			addTF();
			td.setTopic(t, maxTF);
		}
		//System.out.println(topics.length);
	}
	
	/**
	 * 将一个含有多道题目的字符串转化为一个题目数组
	 * @param topic：含有多个题目的字符串
	 * @return
	 */
	private static String[] splitTopics(String topic){
		System.out.println(topic);
		String[] topics = topic.split("（   ）");
		//如果题目的括号为英文版的话导致切割失败，需要使用英文版再切一次
		System.out.println(topics.length);
		return topics;
	}

	/**
	 * 将一套试卷全部输入到数据库中
	 * 
	 * @param topicsFile 试卷的题目文件
	 * @param answersFile 试卷的答案文件
	 * @return
	 * @throws IOException
	 * @throws XmlException
	 * @throws OpenXML4JException
	 */
	public String setAllTopic() throws IOException, XmlException, OpenXML4JException{
		
		//数据准备
		/*
		
		cur_t = cur_t[1].split("三、");
		cur_a = cur_a[1].split("三、");
		String multipleTopic = cur_t[0];
		String multipleAnswer = cur_a[0];
		String TFTopic = cur_t[1];
		String */
		//=========================
		//将单选题输入数据库
		//2016学生校规校纪A卷
		String singletopics_1 = test1.getTopicFromWord_doc("doc2016URL").split("一、单项选择题")[1];
		String singleanswers_1 = test1.getTopicFromWord_docx("docx2016AURL").split("一、")[1];
		String[] cur_t_1 = singletopics_1.split("二、");
		String[] cur_a_1 = singleanswers_1.split("二、");
		setSingleTopic(cur_t_1[0], cur_a_1[0]);
		//2017江西师范大学2017级学生校规校纪知识竞赛试题1118
		String singletopics_2 = test1.getTopicFromWord_doc("doc2017URL").split("一、单项选择题")[1];
		String singleanswers_2 = test1.getTopicFromWord_doc("doc2017AURL").split("一、")[1];
		String[] cur_t_2 = singletopics_2.split("二、");
		String[] cur_a_2 = singleanswers_2.split("二、");
		setSingleTopic(cur_t_2[0], cur_a_2[0]);
		//江西师范大学2018级学生校规校纪考试试题
		/*String singletopics_3 = test1.getTopicFromWord_doc("doc3017URL").split("一、单项选择题")[1];
		String singleanswers_3 = test1.getTopicFromWord_doc("doc3017AURL").split("一、")[1];
		String[] cur_t_3 = singletopics_3.split("二、");
		String[] cur_a_3 = singleanswers_3.split("二、");
		setSingleToipc(cur_t_3[0], cur_a_3[0]);*/
		
		//=========================================
		//将多选题输入数据库
		//2016学生校规校纪A卷
		cur_t_1 = cur_t_1[1].split("三、");
		cur_a_1 = cur_a_1[1].split("三、");
		setMultpleTopic(cur_t_1[0], cur_a_1[0]);
		//2017江西师范大学2017级学生校规校纪知识竞赛试题1118
		cur_t_2 = cur_t_2[1].split("三、");
		cur_a_2 = cur_a_2[1].split("三、");
		setMultpleTopic(cur_t_2[0], cur_a_2[0]);
		
		//==========================================
		//将判断题输入数据库
		//2016学生校规校纪A卷
		setTFTopic(cur_t_1[1], cur_a_1[1]);
		//2117江西师范大学2117级学生校规校纪知识竞赛试题1118
		setTFTopic(cur_t_2[1], cur_a_2[1]);
		
		//将配置计算的各种数据重新输入到配置文件中
		setProperties();
		return null;
	}
	
	/**
	 * 将配置计算的各种数据重新输入到配置文件中
	 */
	private void setProperties() {
		String[] arr = new String[6];
		arr[0] = ""+minSingle;
		arr[1] = ""+maxSingle;
		arr[2] = ""+minMultiple;
		arr[3] = ""+maxMultiple;
		arr[4] = ""+minTF;
		arr[5] = ""+maxTF;
		properties.setProperties(arr);
	}

	public static void main(String[] args) throws IOException, XmlException, OpenXML4JException {
		(new test1()).setAllTopic();
		/*String answer = "61-65 ABCD、ABCD、ABCD、BC、ABC";
		String all = answer.replaceAll("、", ".").replaceAll("[^A-Z\\.]", "");
		System.out.println(all);
		String[] answers = all.split("\\.");
		for(int i=1;i<answers.length;i++){
			System.out.println(answers[i]);
		}*/
		//test1.getTopicFromWord("docx2016AURL");
		/*String str = "91-95 ×√√√√";
		str = str.replaceAll("[0-9a-zA-Z- ]","");
		System.out.print(str);*/
	}

}
